//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.atjg.autocat.uvccamera.usb;

import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.text.TextUtils;

public class UsbController {
    private static final String DEFAULT_USBFS = "/dev/bus/usb";
    private UsbManager mUsbManager;
    private UsbDevice mUsbDevice;
    private UsbDeviceConnection mUsbConnection;

    public UsbController(UsbManager usbManager, UsbDevice usbDevice) {
        this.mUsbManager = usbManager;
        this.mUsbDevice = usbDevice;
    }

    public UsbDeviceConnection open() {
        if (this.mUsbManager != null && this.mUsbDevice != null) {
            this.mUsbConnection = this.mUsbManager.openDevice(this.mUsbDevice);
        }

        return this.mUsbConnection;
    }

    public void close() {
        if (this.mUsbConnection != null) {
            this.mUsbConnection.close();
            this.mUsbConnection = null;
        }

    }

    public UsbDeviceConnection getConnection() {
        return this.mUsbConnection;
    }

    public UsbDevice getUsbDevice() {
        return this.mUsbDevice;
    }

    public int getVendorId() {
        return this.mUsbDevice != null ? this.mUsbDevice.getVendorId() : 0;
    }

    public int getProductId() {
        return this.mUsbDevice != null ? this.mUsbDevice.getProductId() : 0;
    }

    public int getFileDescriptor() {
        return this.mUsbConnection != null ? this.mUsbConnection.getFileDescriptor() : 0;
    }

    public int getBusNum() {
        int busnum = 0;
        if (this.mUsbDevice != null) {
            String name = this.mUsbDevice.getDeviceName();
            String[] info = !TextUtils.isEmpty(name) ? name.split("/") : null;
            if (info != null && info.length > 1) {
                busnum = Integer.parseInt(info[info.length - 2]);
            }
        }

        return busnum;
    }

    public int getDevNum() {
        int devnum = 0;
        if (this.mUsbDevice != null) {
            String name = this.mUsbDevice.getDeviceName();
            String[] info = !TextUtils.isEmpty(name) ? name.split("/") : null;
            if (info != null && info.length > 0) {
                devnum = Integer.parseInt(info[info.length - 1]);
            }
        }

        return devnum;
    }

    public String getUSBFSName() {
        String result = null;
        if (this.mUsbDevice != null) {
            String name = this.mUsbDevice.getDeviceName();
            String[] info = !TextUtils.isEmpty(name) ? name.split("/") : null;
            if (info != null && info.length > 2) {
                StringBuilder sb = new StringBuilder(info[0]);

                for(int i = 1; i < info.length - 2; ++i) {
                    sb.append("/").append(info[i]);
                }

                result = sb.toString();
            }
        }

        return !TextUtils.isEmpty(result) ? result : "/dev/bus/usb";
    }
}
